<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.servicecode" placeholder="业务标识码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.babsm" placeholder="合并编码" clearable></el-input>
      </el-form-item>
            <el-form-item>
        <el-input v-model="dataForm.slh" placeholder="受理号" clearable></el-input>
      </el-form-item>
            <el-form-item>
        <el-input v-model="dataForm.realtyno" placeholder="不动产登记证明号" clearable></el-input>
      </el-form-item>

      <el-form-item>
          <el-select v-model="dataForm.state" clearable placeholder="交易状态">
            <el-option
              v-for="item in options"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button  type="primary" @click="getDataList()">本地查询</el-button>
        <el-button type="primary" @click="export2Excel('table')">导出</el-button> 
        <el-button v-if="isAuth('bdc:bdcnoticeregist:save')" type="primary" @click="printCode()"  :disabled="dataListSelections.length <= 0" >批量打印条形码</el-button>
        <el-button v-if="isAuth('bdc:bdcnoticeregist:save')" type="primary" @click="printCodePs()"  :disabled="dataListSelections.length <= 0" >批量打印品胜条形码(自助)</el-button>
      </el-form-item>



    </el-form>

  <el-table
      :data="dataList"
      border
      id="table"
      style="width: 100%;display:none;">

      <el-table-column
        prop="stateTranslate"
        header-align="center"
        align="center"
        width="120px"
        label="交易状态">
      </el-table-column>

      <el-table-column
        prop="sendtime"
        header-align="center"
        align="center"
        width="160px"
        label="发送时间">
      </el-table-column>
      <el-table-column
        prop="recvtime"
        header-align="center"
        width="160px"
        align="center"
        label="接收时间">
      </el-table-column>

      <el-table-column
        prop="servicecode"
        header-align="center"
        align="center"
        width="180px"
        label="业务标识码">
      </el-table-column>


      <el-table-column
        prop="babsm"
        header-align="center"
        align="center"
        width="170px"
        label="合并编码">
      </el-table-column>


      <el-table-column
        prop="serviceno"
        header-align="center"
        align="center"
        width="170px"
        label="业务件号">
      </el-table-column>
      <el-table-column
        prop="slh"
        header-align="center"
        align="center"
        width="170px"
        label="受理号">
      </el-table-column>


      <el-table-column
        prop="descinfo"
        header-align="center"
        align="center"
        width="160px"
        label="结果描述">
      </el-table-column>
    </el-table>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="stateTranslate"
        header-align="center"
        align="center"
        width="120px"
        label="交易状态">
      </el-table-column>

      <el-table-column
        prop="sendtime"
        header-align="center"
        align="center"
        width="160px"
        label="发送时间">
      </el-table-column>
      <el-table-column
        prop="recvtime"
        header-align="center"
        width="160px"
        align="center"
        label="接收时间">
      </el-table-column>

      <el-table-column
        prop="servicecode"
        header-align="center"
        align="center"
        width="180px"
        label="业务标识码">
      </el-table-column>


      <el-table-column
        prop="babsm"
        header-align="center"
        align="center"
        width="170px"
        label="合并编码">
      </el-table-column>


      <el-table-column
        prop="serviceno"
        header-align="center"
        align="center"
        width="170px"
        label="业务件号">
      </el-table-column>
      <el-table-column
        prop="slh"
        header-align="center"
        align="center"
        width="170px"
        label="受理号">
      </el-table-column>


      <el-table-column
        prop="descinfo"
        header-align="center"
        align="center"
        width="160px"
        label="结果描述">
      </el-table-column>

      <el-table-column
        fixed="left"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">

          <el-button type="text" v-if="(scope.row.state=='51'  || scope.row.state=='54') && isAuth('bdc:bdcnoticeregist:upload')"  size="small"  
            @refreshDataList="getDataList"  @click="toUploadHandle(scope.row.seqno, scope.row.btype)">资料上传</el-button>

          <el-button type="text" v-if="scope.row.state=='52' && isAuth('bdc:bdcnoticeregist:commit')"  size="small"  
            @refreshDataList="getDataList"  @click="toSubmitHandle(scope.row.seqno, scope.row.btype)">审核提交</el-button>

          <el-button type="text"  v-if="(scope.row.state=='00' || scope.row.state=='53') && isAuth('bdc:bdcnoticeregist:save')" 
             size="small" @click="noticeRegisterHandle(scope.row.seqno)">完善登记</el-button>
          <el-button type="text" v-if="yddjEnable(scope.row.state)" size="small" @click="checkbabsm(scope.row.babsm)">办理预抵</el-button>
          <el-button type="text" 
           v-if="scope.row.state=='11' && isAuth('bdc:bdcnoticeregist:save')"
          size="small"
           @click="reCompleteHandle(scope.row.seqno, scope.row.btype)">重新提交</el-button>
          <el-button type="text" size="small"  @click="beforeprocview(scope.row.seqno)">查看进度</el-button>

          <el-button type="text" size="small" v-if="scope.row.state=='25'" 
             @click="certinfoHandle(scope.row.bdcno, scope.row.region, scope.row.servicecode)">电子证明查询</el-button>

          <el-button type="text" v-if="scope.row.slh" size="small"    @refreshDataList="getDataList"  @click="printCodePs(scope.row.seqno)">条形码(自助)</el-button>
          <el-button  type="text" size="small" @click="detailHandle(scope.row.seqno)">显示明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="datepicker.pageOptions"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <notice-register v-if="noticeRegisterVisible" ref="noticeRegister" @refreshDataList="getDataList"></notice-register>
    <procview v-if="procviewVisible" ref="procview"   @refreshDataList="getDataList" ></procview>
    <notice-register-detail v-if="noticeRegisterDetailVisible" ref="noticeRegisterDetail" @refreshDataList="getDataList"></notice-register-detail>
    <print-bar-code v-if="printBarCodeVisible" ref="printBarCode"   @refreshDataList="getDataList" ></print-bar-code> 
    <ygdj-register v-if="ygdjRegisterVisible" ref="ygdjRegister"   @refreshDataList="getDataList" ></ygdj-register>

    <bdc-cert-info  v-if="certinfoVisible" ref="bdcCertInfo" @refreshDataList="getDataList"></bdc-cert-info>

  
  </div>
</template>

<script>
  import AddOrUpdate from './bdcnoticeregist-add-or-update'
  import NoticeRegister from './notice-register'
  import NoticeRegisterDetail from './notice-register-detail'
  import Procview from './bdcprocview' 
  import YgdjRegister from './yddj-register'
  import PrintBarCode from './bdc-printbarcode' 
  import { printPsYg }   from '@/assets/js/printbarcode.js' 
  import BdcCertInfo from './bdccertinfo-add-or-update'
  export default {
    data () {
      return {
        options: [],
        dataForm: {
          state: '',
          servicecode: '',
          babsm: '',
          slh: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        noticeRegisterVisible: false,
        procviewVisible: false,
        noticeRegisterDetailVisible: false,
        ygdjRegisterVisible: false,
        printBarCodeVisible: false,
        certinfoVisible: false
      }
    },
    components: {
      AddOrUpdate,
      NoticeRegister,
      Procview,
      NoticeRegisterDetail,
      YgdjRegister,
      PrintBarCode,
      BdcCertInfo
    },
    activated () {
      this.getDataList()
      this.initOptions()
    },
    methods: {
      export2Excel(id){
          this.datepicker.export2Excel(id, this, '预告登记.xlsx')
      },
      yddjEnable(state){
        
        return (state=='01' || state=='03' ) 
            && this.isAuth('bdc:bdcmortgregist:save') 
      },
            // 新增 / 修改
      certinfoHandle (bdcno, region, servicecode) {
        this.certinfoVisible = true
        this.$nextTick(() => {
          this.$refs.bdcCertInfo.init(-1, bdcno, region, servicecode)
        })
      },

        checkBeforePrint(){
          for (var i = 0; i < this.dataListSelections.length; i++){
            var data = this.dataListSelections[i]
            if (!data.slh){
              return false
            }
          }
          return true
        },
        printCode(){
            if (!this.checkBeforePrint()){
              this.$message.error("只能打印有受理号的记录")
              return
            }

            var ids = this.dataListSelections.map(item => {
              return item.seqno
            })
            if (ids.length == 0){
              this.$message.error("请勾选待打印数据")
              return ;
            }
            this.printBarCodeVisible = true
            this.$nextTick(() => {
              this.$refs.printBarCode.init(ids, this.$http.adornUrl(`/bdc/bdcnoticeregist/printbarcode_ps`))
            })
        },

        printCodePs(id){

          if (!this.checkBeforePrint()){
              this.$message.error("只能打印有受理号的记录")
              return
            }

          var ids = id ? [id] : this.dataListSelections.map(item => {
            return item.seqno
          })

          if (ids.length == 0){
            this.$message.error("请勾选待打印数据")
            return ;
          }
                
          printPsYg(ids, this)
         
      },


      checkbabsm(babsm){
          this.$http({
            url: this.$http.adornUrl(`/bdc/bdcmortgregist/babsm/check/`+babsm),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
               this.showYgdj(babsm)
            }else{
               this.$message.error(data.msg)
            }
          })
      },

      showYgdj(babsm){
          this.ygdjRegisterVisible = true
          this.$nextTick(() => {
            this.$refs.ygdjRegister.init(babsm, 0)
          })
      },

      initOptions(){
          this.$http({
            url: this.$http.adornUrl(`/bdc/bdcstateinfo/item/list`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.options = data.list
          })
      },

      beforeprocview(id){
              this.$http({
                  url: this.$http.adornUrl(`/bdc/bdcprocview/procviewyg/` + id),
                  method: 'get',
                  params: this.$http.adornParams()
                }).then(({data}) => {
                  if (data && data.code === 0) {

                      this.procviewHandle(data.seqno, data.btype, data.servicecode, data.zl, data.seRegion)

                  }
                  else{
                    this.$message.error(data.msg)
                  }
                }).catch(err => {
                 this.$message.error("系统内部错误")
                 console.error(err);            
              }); 
      },

      procviewHandle(seqno, btype, servicecode, zl, seRegion){
        this.procviewVisible = true
        this.$nextTick(() => {
          this.$refs.procview.init(seqno, btype, servicecode, zl, seRegion)
        })
      },



      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/bdc/bdcnoticeregist/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'realtyno': this.dataForm.realtyno,
            'servicecode': this.dataForm.servicecode,
            'slh': this.dataForm.slh,
            'babsm': this.dataForm.babsm,
            'state': this.dataForm.state
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },

      detailHandle (id) {
        this.noticeRegisterDetailVisible = true
        this.$nextTick(() => {
          this.$refs.noticeRegisterDetail.init(id, 1)
        })
      },

      reCompleteHandle (id) {
        this.noticeRegisterVisible = true
        this.$nextTick(() => {
          this.$refs.noticeRegister.init(id, 2)
        })
      },

      

      toSubmitHandle(id){
          this.noticeRegisterVisible = true
          this.$nextTick(() => {
            this.$refs.noticeRegister.init(id, 52)
          })
      },

      toUploadHandle(id){
          this.noticeRegisterVisible = true
          this.$nextTick(() => {
            this.$refs.noticeRegister.init(id, 51)
          })
      },

      noticeRegisterHandle (id) {
        this.noticeRegisterVisible = true
        this.$nextTick(() => {
          this.$refs.noticeRegister.init(id, 1)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.seqno
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/bdc/bdcnoticeregist/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style lang="scss">
body .el-table th.gutter{
    display: table-cell!important;

}
</style>